<?php

namespace app\admin\controller;

use think\Db;

class Slide extends Common
{
    public function index()
    {
        $data = Db::name('lunbo_type')->order('des desc,id asc')->select();

        $this->assign('data', $data);

        return $this->fetch();
    }

    // 分组
    public function typeadd()
    {
        $id = input("id");

        if (request()->isAjax()) {

            $post = input('post.');
            $time = time();

            $post['update_time'] = $time;

            if (!$id) {
                $category = Db::name('lunbo_type')->where(['title' => $post['title']])->find();
                if ($category) return ['err' => '分组标题已存在'];

                $post['create_time'] = $time;
                $result = Db::name('lunbo_type')->insert($post);

                $data = $result ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $result = Db::name('lunbo_type')->where('id', $id)->update($post);

                $data = $result ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('lunbo_type')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    // 删除分组
    public function typeDel()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $result = Db::name('lunbo_type')->delete($id);

                $data = $result ? ['ret' => '删除成功'] : ['err' => '删除失败'];
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    // 轮播图列表
    public function lists()
    {
        $tid = input('tid');

        $data = Db::name('slides')->where(['tid' => $tid])->order('des desc,id desc')->select();

        $this->assign('tid', $tid);
        $this->assign('data', $data);

        return $this->fetch('list');
    }

    // 显示 隐藏轮播图
    public function hide()
    {
        $id = input('id');

        if (request()->isAjax()) {

            if ($id) {
                $arra = ['0' => '隐藏', '1' => '显示'];
                $type = 'hide';
                $db = Db::name('slides')->where('id', $id)->value('hide');
                if ($db == 1) {
                    $hide = 0;
                } else {
                    $hide = 1;
                }
                $db = Db::name('slides')->where('id', $id)->setField($type, $hide);
                if ($db) {
                    $data = ['ret' => $arra[$hide] . '成功', 'id' => $hide, 'text' => $arra[$hide]];
                } else {
                    $data = ['err' => $arra[$hide] . '失败'];
                }
            } else {
                $data = ['err' => 'ID参数错误'];
            }
        } else {
            $data = ['err' => 'AJAX参数错误'];
        }
        return $data;
    }

    // 添加图片
    public function add()
    {
        $id = input("id");
        $tid = input("tid");

        $this->assign('tid', $tid);
        $slide_type = Db::name('lunbo_type')->select();
        $this->assign('lunbo_type', $slide_type);

        if (request()->isAjax()) {

            $post = input('post.');

            $post['tid'] = $tid;

            if (!$id) {
                $result = Db::name('slides')->insert($post);

                $data = $result ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $result = Db::name('slides')->where('id', $id)->update($post);

                $data = $result !== false ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('slides')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }

    // 删除图片
    public function del()
    {
        if (request()->isAjax()) {

            $id = input('id');
            $tid = input('tid');

            if ($id) {
                $result = Db::name('slides')->delete($id);

                $data = $result ? ['ret' => '删除成功'] : ['err' => '删除失败'];
            } else {
                $data = ['err' => '参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
}
